package com.ryder.ryderschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ryder.ryderschool.entity.Project;
import com.ryder.ryderschool.entity.ProjectMember;
import com.ryder.ryderschool.entity.ProjectResult;
import com.ryder.ryderschool.service.ProjectMemberService;
import com.ryder.ryderschool.service.ProjectResultService;
import com.ryder.ryderschool.service.ProjectService;
import com.ryder.ryderschool.service.StatisticsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class StatisticsServiceImpl implements StatisticsService {

    private final ProjectService projectService;
    private final ProjectMemberService projectMemberService;
    private final ProjectResultService projectResultService;

    @Override
    public Map<String, Object> getDepartmentStatistics() {
        List<Project> projects = projectService.list();
        Map<Integer, List<Project>> deptProjects = projects.stream()
                .collect(Collectors.groupingBy(Project::getDeptId));

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> deptStats = new HashMap<>();

        deptProjects.forEach((deptId, deptProjectList) -> {
            Map<String, Object> stats = new HashMap<>();
            stats.put("projectCount", deptProjectList.size());
            stats.put("totalBudget", deptProjectList.stream()
                    .map(Project::getBudget)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            deptStats.put(deptId.toString(), stats);
        });

        result.put("departmentStatistics", deptStats);
        return result;
    }

    @Override
    public Map<String, Object> getProjectTypeStatistics() {
        List<Project> projects = projectService.list();
        Map<String, List<Project>> typeProjects = projects.stream()
                .collect(Collectors.groupingBy(Project::getType));

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> typeStats = new HashMap<>();

        typeProjects.forEach((type, typeProjectList) -> {
            Map<String, Object> stats = new HashMap<>();
            stats.put("projectCount", typeProjectList.size());
            stats.put("totalBudget", typeProjectList.stream()
                    .map(Project::getBudget)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            typeStats.put(type, stats);
        });

        result.put("projectTypeStatistics", typeStats);
        return result;
    }

    @Override
    public Map<String, Object> getYearlyStatistics() {
        List<Project> projects = projectService.list();
        Map<Integer, List<Project>> yearlyProjects = projects.stream()
                .collect(Collectors.groupingBy(p -> p.getCreatedAt().getYear()));

        Map<String, Object> result = new HashMap<>();
        Map<String, Object> yearlyStats = new HashMap<>();

        yearlyProjects.forEach((year, yearProjectList) -> {
            Map<String, Object> stats = new HashMap<>();
            stats.put("projectCount", yearProjectList.size());
            stats.put("totalBudget", yearProjectList.stream()
                    .map(Project::getBudget)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            yearlyStats.put(year.toString(), stats);
        });

        result.put("yearlyStatistics", yearlyStats);
        return result;
    }

    @Override
    public Map<String, Object> getTeacherStatistics(Integer teacherId) {
        // 获取教师参与的项目
        List<ProjectMember> memberships = projectMemberService.list(
                new LambdaQueryWrapper<ProjectMember>()
                        .eq(ProjectMember::getUserId, teacherId));

        List<Integer> projectIds = memberships.stream()
                .map(ProjectMember::getProjectId)
                .collect(Collectors.toList());

        // 获取项目成果
        List<ProjectResult> results = projectResultService.list(
                new LambdaQueryWrapper<ProjectResult>()
                        .in(ProjectResult::getProjectId, projectIds));

        Map<String, Object> result = new HashMap<>();
        result.put("projectCount", projectIds.size());
        result.put("resultCount", results.size());
        result.put("resultTypes", results.stream()
                .collect(Collectors.groupingBy(ProjectResult::getType, Collectors.counting())));

        return result;
    }
} 